package com.javabase.study.ArrayStudy;

import java.util.Arrays;

/**
 * TODO
 * 内容：测试常用的Arrays常用方法
 * 作者:jxx
 * 创建时间：2021/6/23
 */
public class TestArrayClassMethod {
    public static void main(String[] args){

        int[] a = {1,6,3,4,2,9,7};
        // 数组打印
        System.out.println(Arrays.toString(a));

        //数组元素的排序
         Arrays.sort(a);
        System.out.println(Arrays.toString(a));

        //数组元素是引用类型，则引用类必须实现Comparable接口，实现CompareTo方法
        Man son = new Man("son",24);
        Man father = new Man("father",45);

        Man[] mans = {father,son};
        Arrays.sort(mans);
        for (Man man:mans){
            System.out.println(man.getAge());
        }

        //二分法查找
        int r = Arrays.binarySearch(a,6);
        System.out.println(r);

        // 数组填充
        Arrays.fill(a,2,3,5);
        System.out.println(Arrays.toString(a));

        // 使用二维数组保存表格数据
        // 此处 1001\1002\1003 本质不再是基本类型数据，Java编译器会自动把基本数据类型
        //自动装箱称包装类对象
        Object[] a1 = {1001,"蒋星星","数学"};
        Object[] a2 = {1002,"蒋兴","语文"};
        Object[] a3 = {1003,"高杨","计算机"};

        Object[][] objects = new Object[3][];
        objects[0] = a1;
        objects[1] = a2;
        objects[2] = a3;

        for (Object[] obj : objects){
            System.out.println(Arrays.toString(obj));
        }
    }
}

class Man implements Comparable{
    private String name;
    private int age;



    public  Man(String name,int age){
        super();
        this.name = name;
        this.age = age;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setAge(int age){
        this.age = age;
    }

    public String getName(){
        return this.name;
    }

    public int getAge(){
        return this.age;
    }

    @Override
    public int compareTo(Object o) {
        Man man = (Man)o;

        if (this.age == man.age){
            return 0;
        }else if(this.age > man.age){
            return 1;
        }else {
            return -1;
        }

    }
}
